﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Configuration;

namespace GL_GameHungTrung
{
    public enum MoveType
    {
        M_LEFT, M_RIGHT
    }

    public enum PoultryType
    {
        P_DUCK, P_CHICKEN, P_GOOSE
    }
    public class Poultry
    {
        #region Private Variables

        private MoveType _mt;
        private Image _img;
        private int _x;
        private int _y;
        private int _height;
        private int _width;
        private int _speed;
        private int _timeLife;
        private int _timeExcrete;
        private int _bossPeriod;


        #endregion

        // Sự kiện đẻ trứng
        public delegate void excreteEggShit_Handler(Point p, FallerType ft);
        public event excreteEggShit_Handler excreteEggShit;
        protected void ExcreteEggShit()
        {
            if (excreteEggShit != null)
            {
                //FallerType ft;
                int ran;
                Random rd = new Random();

                if (TimeLife > 0 && TimeLife % _bossPeriod == 0)
                {
                    ran = rd.Next(0, 2);
                    switch(ran)
                    {
                        case 0: excreteEggShit(new Point(X, Y), FallerType.EGG_PINK); break;
                        case 1: excreteEggShit(new Point(X, Y), FallerType.EGG_RED); break;
                        case 2: excreteEggShit(new Point(X, Y), FallerType.EGG_GOLD); break;
                    }
                }
                else
                {
                    ran = rd.Next(0, 9);
                    if (ran == 0 || ran == 1 || ran == 2)
                        excreteEggShit(new Point(X, Y), FallerType.SHIT);
                    else
                        excreteEggShit(new Point(X, Y), FallerType.EGG_BLUE);
                }

                //excreteEggShit(new Point(X,Y), ft);
            }
        }

        #region Properties

        public int X
        {
            set
            {
                _x = value;
            }
            get
            {
                return _x;
            }
        }

        public int Y
        {
            set
            {
                _y = value;
            }
            get
            {
                return _y;
            }
        }

        public int Height
        {
            set
            {
                _height = value;
            }
            get
            {
                return _height;
            }
        }

        public int Width
        {
            set
            {
                _width = value;
            }
            get
            {
                return _width;
            }
        }

        public int Speed
        {
            set
            {
                _speed = value;
            }
            get
            {
                return _speed;
            }
        }

        public int TimeExcrete
        {
            set
            {
                _timeExcrete = value;
            }
            get
            {
                return _timeExcrete;
            }
        }

        public int TimeLife
        {
            set
            {
                _timeLife = value;
            }
            get
            {
                return _timeLife;
            }
        }

        public int BossPeriod
        {
            set
            {
                _bossPeriod = value;
            }
            get
            {
                return _bossPeriod;
            }
        }

        public  MoveType MType
        {
            set
            {
                _mt = value;
            }
            get
            {
                return _mt;
            }
        }

        #endregion

        public Poultry(PoultryType pt)
        {
            _mt = MoveType.M_LEFT;   
            switch(pt)
            {
                case PoultryType.P_DUCK: _img = Properties.Resources.vit; break;
                case PoultryType.P_GOOSE: _img = Properties.Resources.ngan; break;
                case PoultryType.P_CHICKEN: _img = Properties.Resources.ga; break;
            }
        }

        public void DrawFaller(Graphics g)
        {
            g.DrawImage(_img,_x,_y,Width,Height);
        }

        public void DrawFaller(Graphics g, int move_x, int move_y)
        {
            _x = move_x;
            _y = move_y;
            DrawFaller(g);
        }

        public void Crossing(int screenWidth, int poultryExcrete)
        {
            if (X >= 0)
            {
                if (MType == MoveType.M_LEFT)
                {
                    X = X - Speed;
                    if (X <= 180) // Ball Touches Bottom
                    {
                        X = 180;
                        MType = MoveType.M_RIGHT;
                    }                    
                }else if (MType == MoveType.M_RIGHT)
                {
                    X = X + Speed;
                    if (X >= 420-Width) // Ball Touches Bottom
                    {
                        X = 420-Width;
                        MType = MoveType.M_LEFT;
                    }
                }
            }

            // Đếm giây
            TimeExcrete ++;
            // Kích hoạt sự kiện đẻ trứng
            if (TimeExcrete >= poultryExcrete)
            {
                TimeExcrete = 0;
                ExcreteEggShit();
            }

        }
    }
}
